Skip to content

Conversation

black-06
Copy link

Adds a module that allows subtype to be registered without annotating the parent class.

It is implemented on SPI.

See FasterXML/jackson-databind#2104

Its original version is https://github.com/black-06/jackson-modules-dynamic-subtype.

I removed the custom ServiceLoader and always use the standard library,
which means that pojo always needs a no-parameter constructor.

@@ -33,6 +33,7 @@ not datatype, data format, or JAX-RS provider modules.
<module>mrbean</module>
<module>osgi</module>
<module>paranamer</module>
<module>subtype</module>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll probably want to change the name, "subtype" is too generic. But let me think about better name for a while...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a difficult thing for me to give it a name...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True🥲🥲

import static org.junit.Assert.assertTrue;

/**
* test work without {@link JsonSubTypes}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure I understand -- why would we test functioning without module? And isn't module actually register with the mapper. Or what is being tested here? Could probably use longer explanation of reasoning.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably test against Jackson polymorphic type handling working as expected. If so, does not seem necessary

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually wanted to test whether @JsonSubType worked with @JsonSubTypes

Comment on lines 53 to 54
List<NamedType> list1 = SubtypeModule.findSubtypes(a.getRawType(), a::getAnnotation);
List<NamedType> list2 = subtypes.getOrDefault(a.getRawType(), Collections.emptyList());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we rename these list1 and list2 to something less generic?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +56 to +57
if (list1.isEmpty()) return list2;
if (list2.isEmpty()) return list1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the two lists are null-safe, no need for empty checking.

Comment on lines +58 to +61
List<NamedType> list = new ArrayList<>(list1.size() + list2.size());
list.addAll(list1);
list.addAll(list2);
return list;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
List<NamedType> list = new ArrayList<>(list1.size() + list2.size());
list.addAll(list1);
list.addAll(list2);
return list;
List<NamedType> list = new ArrayList<>(list1.size() + list2.size());
list1.addAll(list2);
return list;

Comment on lines +58 to +61
List<NamedType> list = new ArrayList<>(list1.size() + list2.size());
list.addAll(list1);
list.addAll(list2);
return list;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to have duplication? If so, can we have these as set first, then return as list?

import static org.junit.Assert.assertTrue;

/**
* test work without {@link JsonSubTypes}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably test against Jackson polymorphic type handling working as expected. If so, does not seem necessary

subtypes.remove(parent);
}

private static <S> List<NamedType> findSubtypes(Class<S> clazz, Function<Class<JsonSubType>, JsonSubType> getter) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason for keeping it static? if not maybe like...

Suggested change
private static <S> List<NamedType> findSubtypes(Class<S> clazz, Function<Class<JsonSubType>, JsonSubType> getter) {
private <S> List<NamedType> _findSubtypes(Class<S> clazz, Function<Class<JsonSubType>, JsonSubType> getter) {

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotation
public @interface JsonSubType {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Name sounds like it's part of @JsonSubTypes of annotations module. This may confuse users quite. If module name changes as per https://github.com/FasterXML/jackson-modules-base/pull/229/files#r1390329649, can we change name here also?

* more than one type name should be associated with the same type.
*
* @return subtype name array
* @since 2.12
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Version mismatch, can remove.

Suggested change
* @since 2.12

... then add @since 2.16 to the class

Comment on lines 25 to 27
* Therefore, we can {@link #unregisterType} a class and then module will reload this class's subclasses.
*/
public class SubtypeModule extends Module {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Therefore, we can {@link #unregisterType} a class and then module will reload this class's subclasses.
*/
public class SubtypeModule extends Module {
* Therefore, we can {@link #unregisterType} a class and then module will reload this class's subclasses.
*
* @since 2.16
*/
public class SubtypeModule extends Module {

@cowtowncoder cowtowncoder changed the title feat: add subtype module feat: add subtype module (2.17) Nov 16, 2023
@cowtowncoder
Copy link
Member

Quick note: due to timing, decided that I will not try to get this in Jackson 2.16 -- so will need to be re-based to 2.17.
This because there's a bit of work to be done here.

For example, I think that naming of @JsonSubType should change as the general rule is that only jackson-annotations has annotations with name starting with @Json (and for legacy reasons, some in jackson-databind).
Modules can provide annotations that start with @Jackson prefix (or anything else that makes sense).

But I need to read the PR in bit more thought to make sure my feedback is relevant.

@amricko0b
Copy link

Hello everyone!
Any updates on this one?

@cowtowncoder
Copy link
Member

I totally forgot. If this was to be added, would need to re-base/re-create against 2.x branch.

@black-06 black-06 changed the base branch from 2.16 to 2.x September 4, 2025 00:57
@black-06
Copy link
Author

black-06 commented Sep 4, 2025

I totally forgot. If this was to be added, would need to re-base/re-create against 2.x branch.

I've changed base to 2.x branch, and I checked the comments above, most of them have been modified, and the problems that haven't been solved include:

  • findSubtypes: It's almost a copy of AnnotationIntrospectorPair.findSubtypes, so I tend not to modify it.
  • annotation name JsonSubType: If the user understands JsonSubTypes, then he should be able to understand JsonSubType easily, but if there is a more appropriate name, I have no problem.

Because it's been a while. We can review the code again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants